"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const FairyGUI = CS.FairyGUI;
const FairyEditor = CS.FairyEditor;
const System = CS.System;
const UIWind_1 = require("./UIWind");
const ToolMgr_1 = require("../ToolMgr");
const App = FairyEditor.App;
class FindDependencyQuery extends UIWind_1.default {
    constructor() {
        super(new UIWind_1.WindData("Extend", "FindDependencyQuery"));
    }
    static show() {
        let url = ToolMgr_1.default.joinUrl("Extend", "FindDependencyQuery");
        if (UIWind_1.default.FindUrl(url) == undefined) {
            UIWind_1.default.add(new this);
        }
        super.show(url);
    }
    onInit() {
        super.onInit();
        this.result = this.contentPane.GetChild("result").asTextField;
        this.list1 = this.contentPane.GetChild("list1").asList;
        this.list2 = this.contentPane.GetChild("list2").asList;
        this.list3 = this.contentPane.GetChild("list3").asList;
        this.contentPane.GetChild("close").onClick.Add(this.onCloseClick.bind(this));
        this.list1.itemRenderer = this.onList1ItemRenderer.bind(this);
        this.list2.itemRenderer = this.onList2ItemRenderer.bind(this);
        this.list1.onClickItem.Add(this.onList1ClickItem.bind(this));
        this.list2.onClickItem.Add(this.onList2ClickItem.bind(this));
        this.list3.itemRenderer = this.onList3ItemRenderer.bind(this);
        this.list3.onClickItem.Add(this.onList3ClickItem.bind(this));
        this.list3.SetVirtual();
    }
    onShown() {
        super.onShown();
        this.onUpdateShown();
    }
    onHide() {
        super.onHide();
        this.list1.numItems = 0;
        this.list2.numItems = 0;
        this.list1.data = null;
        this.list2.data = null;
        this.list3.numItems = 0;
        this.list3.data = null;
    }
    onUpdateShown() {
        let pkgs = [];
        for (let index = 0; index < App.project.allPackages.Count; index++) {
            pkgs.push(App.project.allPackages.get_Item(index));
        }
        this.list1.data = pkgs;
        this.list1.numItems = pkgs.length;
        let dic = ToolMgr_1.default.createGenericDictionary(System.String, System.String);
        dic.Add("count", pkgs.length + "");
        this.result.templateVars = dic;
    }
    onList1ClickItem(item) {
        let querys = new Array();
        let pkg = item.data.data;
        let count = pkg.items.Count;
        let list3Array = new Array();
        for (let index = 0; index < count; index++) {
            let item = pkg.items.get_Item(index);
            if (item.type != FairyEditor.FPackageItemType.COMPONENT) {
                continue;
            }
            list3Array.push(item);
            let list = ToolMgr_1.default.createGenericList(FairyEditor.FPackageItem);
            list.Add(item);
            let data = new FairyEditor.DependencyQuery();
            data.QueryDependencies(list, FairyEditor.DependencyQuery.SeekLevel.SAME_PACKAGE);
            for (let index = 0; index < data.references.Count; index++) {
                let reference = data.references.get_Item(index);
                let pkgID = reference.pkgId;
                let yilaipkg = App.project.GetPackage(pkgID);
                if (yilaipkg == null)
                    continue;
                if (yilaipkg.GetItem(reference.itemId) && querys.indexOf(yilaipkg.name) == -1 && yilaipkg.name != pkg.name) {
                    querys.push(yilaipkg.name);
                }
            }
        }
        this.list2.data = querys;
        this.list2.numItems = querys.length;
        this.list3.data = list3Array;
        this.list3.numItems = list3Array.length;
    }
    onList2ClickItem(index, item) {
    }
    onList3ClickItem(obj) {
        let querys = new Array();
        let item = obj.data.data;
        let list = ToolMgr_1.default.createGenericList(FairyEditor.FPackageItem);
        list.Add(item);
        let data = new FairyEditor.DependencyQuery();
        data.QueryDependencies(list, FairyEditor.DependencyQuery.SeekLevel.ALL);
        for (let index = 0; index < data.references.Count; index++) {
            let reference = data.references.get_Item(index);
            let pkgID = reference.pkgId;
            let yilaipkg = App.project.GetPackage(pkgID);
            if (yilaipkg == null)
                continue;
            if (yilaipkg.GetItem(reference.itemId) && querys.indexOf(yilaipkg.name) == -1) {
                querys.push(yilaipkg.name);
            }
        }
        this.list2.data = querys;
        this.list2.numItems = querys.length;
        App.consoleView.Log(ToolMgr_1.default.getUBBUrl(item.GetURL(), item.name));
    }
    onList1ItemRenderer(index, obj) {
        let data = this.list1.data[index];
        obj.text = data.name;
        obj.data = data;
        obj.__onDispose = () => {
            obj.text = "";
            obj.data = null;
        };
    }
    onList2ItemRenderer(index, obj) {
        let data = this.list2.data[index];
        obj.text = data;
        obj.data = data;
        obj.__onDispose = () => {
            obj.text = "";
            obj.data = null;
        };
    }
    onList3ItemRenderer(index, obj) {
        let data = this.list3.data[index];
        obj.text = data.name;
        obj.data = data;
        obj.__onDispose = () => {
            obj.text = "";
            obj.data = null;
        };
    }
    onCloseClick() {
        let self = this;
        self.Hide();
        FairyGUI.Timers.inst.Add(0.05, 1, () => {
            UIWind_1.default.remove(this);
        });
    }
}
exports.default = FindDependencyQuery;
